//
//  HYDeviceTypeViewController.m
//  TJHYHardwareProduct
//
//  Created by Kendama on 2018/1/24.
//  Copyright © 2018年 Kendama. All rights reserved.
//

#import "HYDeviceTypeVC.h"
#import "HYDeviceTypeListApi.h"
@interface HYDeviceTypeVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSArray *dataSource;
@property(nonatomic,assign)NSInteger index;

@end

@implementation HYDeviceTypeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.index = -1;
    [self getDeviceTypeList];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

#pragma mark --- 点击事件
- (void)onRightButtonClick:(id)sender
{
    if (self.deviceTypeBlock)
    {
        self.deviceTypeBlock(self.dataSource[_index]);
    }
}


#pragma mark --- 事件处理
- (void)getDeviceTypeList
{
    [HYDeviceTypeListApi getDeviceType:^(NSArray<HYDeviceTypeModel *> *list) {
        self.dataSource = list;
        [self.tableView reloadData];
    } fail:^(NSDictionary *dic) {
        [SVProgressHUD showErrorWithStatus:dic[@"data"][@"errmsg"]];
    }];
}

#pragma mark --- 代理
#pragma mark - --------------------------UITableView dataSource--------------------------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
    }
    cell.textLabel.font = fontWithSize(15);
    HYDeviceTypeModel *model = self.dataSource[indexPath.row];
    cell.textLabel.text = model.name;
    if (self.index == indexPath.row)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
//    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}
#pragma mark - --------------------------UITableView delegate--------------------------
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return px1920Height(150);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    _index = indexPath.row;
    [self.tableView reloadData];
}

#pragma mark -- 懒加载
- (UITableView *)tableView
{
    if(!_tableView)
    {
        UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [superView addSubview:_tableView];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}
- (NSArray *)dataSource
{
    if(!_dataSource)
    {
        _dataSource = @[];
    }
    return _dataSource;
}

#pragma mark --- 布局
- (void)base_masLayoutSubview
{
    __weak UIView *superView = self.view;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(64);
        make.left.right.bottom.mas_equalTo(superView);
    }];
}

#pragma mark --- 导航头
- (void)base_configNavigationBar
{
    [self setNavigationTitle:@"设备类型" titleColor:[UIColor blackColor]];
    [self setLeftButtonImage:[UIImage imageNamed:@"left"]];
    self.navigationBar.backgroundColor = [UIColor whiteColor];
    [self setRightButtonTitle:@"完成"];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
